<?php

class Juicios_IndexController extends Zend_Controller_Action {

    public function init() {
        
        $auth = Zend_Auth::getInstance();

        if ($auth->hasIdentity()) {
            if (!empty($auth->getIdentity()->id)) {
                $usuarioDao = new Doctrine_Model_UsuariosDoctrineDao();
                $this->view->usuario = $usuarioDao->obtenerPorId($auth->getIdentity()->id);
            }
        } else {
            $this->_helper->redirector('index', 'login', 'admin');
        }
        
        $this->_flashMessenger = $this->_helper->FlashMessenger;
        
        /* Colores */
        $colores = array(
            '5b92d6' => array('5b92d6', '8fbaef'), //azul
            'd6d65b' => array('d6d65b', 'bfbf3f'), //amarillo
            '51ad56' => array('51ad56', '73be78'), //verde
            'db6f70' => array('db6f70', 'e59899'), //rojo
            '5e71a0' => array('5e71a0', '8e9cbc'), //azul oscuro
            'd68e5b' => array('d68e5b', 'e8a97c'), //naranja
            '92989f' => array('92989f', 'bbbec3')); //gris
        $this->view->colorSucursal = $colores[$this->view->usuario->getSucursal()->getColor()];
        /* Colores */
        
        $this->_session = new Zend_Session_Namespace('tema');

        if ($this->_session->tema) {
            $this->view->logo = 'logoVerde.jpg';
            $this->view->colorFondo = '#4D9473';
            $this->view->colorFuente = '#FFF';
            $this->view->colorFuenteContenido = '#000';
            $this->view->colorContenido = '#C6DC94';
            $this->view->colorMenu = 'navbar-inverse';
            $this->view->colorFondoMenuLateral = '#DFDFDF';
        } else {
            $this->view->logo = 'logoBlanco.jpg';
        }
        
        $this->_personaDao = new Doctrine_Model_PersonaDao();
    }

    public function indexAction() {
        
    }
    
    public function registrarAction(){
        
        $this->view->titulo = "Registrar Juicios";
        
        $id = $this->getRequest()->getParam("cliente_id", null);
        $juicio_id = $this->getRequest()->getParam("juicio_id", null);
        $cobro_id = $this->getRequest()->getParam("cobro_id", null);
        
        $cliente = null;
        if ($id):
            $cliente = $this->_personaDao->obtenerPorId($id);
        else:
            $this->_flashMessenger->addMessage(array("success" => "No existe la persona."));
            $this->_helper->redirector('persona', 'busqueda', 'usuarios');
        endif;
        
        $juicio = null;
        
        if ($juicio_id):
            $this->view->titulo = "Datos Juicios";
            $juicio = true;
            
             $juicio = $this->getRequest()->getParams();
             
            
        endif;
        $hoy=new DateTime('now');
        $juicio['fecha_inicio']=$hoy->format('d/m/Y');
        
        $this->view->cliente = $cliente;
        $this->view->juicio = $juicio;
        $this->view->cobro = $cobro_id;
        $this->view->mensajes = $this->_flashMessenger->getMessages();
        
    }
    
    public function guardarAction() {
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        
        $id = $this->getRequest()->getParam("cliente_id", null);
        
        if ($this->getRequest()->isPost()):
            
            $datos = $this->getRequest()->getParams();
        
            $datos["fecha_inicio"] = str_replace("/", "-", $datos["fecha_inicio"]);
        
            if (isset($datos["guardar"])):
                
                $juicio_id = 1;
                
                $params = array("cliente_id" => $id, "juicio_id" => $juicio_id);
                
                $params = array_merge($params, $datos);
            
                $this->_flashMessenger->addMessage(array("success" => "Juicio registrado."));
                $this->_helper->redirector('registrar', 'index', 'juicios', $params);
            elseif (isset($datos["actualizar"])):
                $this->_flashMessenger->addMessage(array("success" => "Juicio actualizado."));
                $this->_helper->redirector('registrar', 'index', 'juicios', array("cliente_id" => $id));
            endif;
            
        endif;
        
        $this->_flashMessenger->addMessage(array("error" => "Error al intentar registrar el credito."));
        $this->_helper->redirector('registrar', 'index', 'juicios');
        
    }
    
    public function registrarcobroAction() {
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        
        if ($this->getRequest()->isPost()):
            
            $id = $this->getRequest()->getParam("cliente_id");
            $cobro_id = $this->getRequest()->getParam("cobro_id");
            
            $datos = $this->getRequest()->getParams();
        
            if (isset($datos["guardar"])):
                $this->_flashMessenger->addMessage(array("success" => "Cobro registrado."));
                $this->_helper->redirector('obternerdatospersona', 'busqueda', 'usuarios', array("persona_id" => $id));
                //$this->_helper->redirector('registrar', 'index', 'juicios', array("cliente_id" => $id));
            elseif (isset($datos["actualizar"])):
                $this->_flashMessenger->addMessage(array("success" => "Cobro actualizado."));
                $this->_helper->redirector('registrar', 'index', 'juicios', array("cliente_id" => $id));
            endif;
            
        endif;
        
        $this->_flashMessenger->addMessage(array("error" => "Error al intentar registrar el cobro."));
        $this->_helper->redirector('registrar', 'index', 'juicios', array("cliente_id" => $id));
        
    }
    
     public function listadoAction() {
        
    }
    public function nuevapromesaAction() {
       
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.maskedinput-1.3.min.js');

        $this->view->cliente_id = $this->getRequest()->getParam("cliente_id");
        $fecha_actual = new DateTime();
        $this->view->fecha_actual = $fecha_actual->format("d/m/Y");

        $unaPersona = new Doctrine_Model_Entities_Persona();
      
    }


}

